﻿using System;
using System.Collections.Generic;

namespace ZYing.QPanel.Impl
{
    public partial class InProcExecutionHistoryStore
    {
        private class Node
        {
            public string Id { get; }
            public Node(ExecutionHistoryEntry entry)
            {
                this.Id = entry.FireInstanceId;
                this._entry = entry;
            }
            private ExecutionHistoryEntry _entry;
            private List<ExecutionLogEntry> _logs;
            public ExecutionHistoryEntry Entry => _entry;
            public void Save(ExecutionHistoryEntry entry)
            {
                if (entry == null) throw new ArgumentNullException(nameof(entry));
                if (entry.FireInstanceId != this.Id) throw new ArgumentException(nameof(entry));
                this._entry = entry;
            }
            public void PushLog(ExecutionLogEntry log)
            {
                lock (this)
                {
                    if (_logs == null) _logs = new List<ExecutionLogEntry>();
                    while (_logs.Count > Limit) _logs.RemoveAt(0);
                    _logs.Add(log);
                }
            }
            public IReadOnlyList<ExecutionLogEntry> GetLogs()
            {
                if (_logs == null) return Array.Empty<ExecutionLogEntry>();
                return _logs;
            }
            public ExecutionLogEntry FindLastLog()
            {
                if (_logs == null || _logs.Count == 0) return null;
                return _logs[_logs.Count - 1];
            }
        }
    }
}
